<html>
<head><meta charset="utf-8"><title>Why is rust-gpu sound ? (it isn&#x27;t yet) · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html">Why is rust-gpu sound ? (it isn&#x27;t yet)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238966827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238966827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238966827">(May 16 2021 at 10:28)</a>:</h4>
<blockquote>
<p>discord requires a email, it doesn't have to be your real main email.</p>
</blockquote>
<p>Zulip supports <a href="http://temp-mail.org">temp-mail.org</a> and other temporary email providers. Discord bans those.</p>



<a name="238966898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238966898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238966898">(May 16 2021 at 10:29)</a>:</h4>
<blockquote>
<p>the non-atomic synchronization would require specialized wrapper types that control the kinds of indices you can use to read/write, IIRC. </p>
</blockquote>
<p>Is that explained somewhere ? (where are you recalling this from?)</p>



<a name="238966974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238966974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238966974">(May 16 2021 at 10:30)</a>:</h4>
<blockquote>
<p>ight now you have to use &amp;mut [u32] or w/e anyway</p>
</blockquote>
<p>I think this code ought to at least be using <code>*mut [u32]</code> to avoid instant UB due to the multiple <code>&amp;mut</code> issue.</p>



<a name="238967012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238967012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238967012">(May 16 2021 at 10:31)</a>:</h4>
<p>This all seems very tricky, so its hard to blame this code for it.</p>



<a name="238967076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238967076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238967076">(May 16 2021 at 10:32)</a>:</h4>
<p>It would be much better if the compiler just rejects <code>&amp;mut</code> in compute shader signatures that can use more than one "thread". IIUC we are all in agreement that this is and will always be unsound, so better start rejecting that kind of code now.</p>



<a name="238967541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/238967541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#238967541">(May 16 2021 at 10:40)</a>:</h4>
<p>Going through the Rust threads about compute shaders, it seems that people are unaware of this. Is there an issue on Rust GPU on github tracking this?</p>



<a name="239011920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239011920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Mobius <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239011920">(May 16 2021 at 22:42)</a>:</h4>
<p><a href="https://github.com/EmbarkStudios/rust-gpu/issues/249">https://github.com/EmbarkStudios/rust-gpu/issues/249</a><br>
This one seems to go in the direction of using something else instead of mut references.</p>



<a name="239115030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239115030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239115030">(May 17 2021 at 15:58)</a>:</h4>
<p>What's the difference between that and just using <code>NonNull&lt;[T]&gt;</code> ?</p>



<a name="239115317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239115317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239115317">(May 17 2021 at 15:59)</a>:</h4>
<p>I guess I don't understand why one has to wait for a <code>Buffer</code> type to be introduced.</p>
<p>You can use <code>usize</code> or <code>*mut [T]</code> or <code>NonNull&lt;[T]&gt;</code> today and that would be sound.</p>
<p>But there is no <code>T</code> for which using <code>&amp;mut T</code> isn't instant undefined behavior (unless the compute shader only has 1 thread).</p>



<a name="239115868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239115868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239115868">(May 17 2021 at 16:02)</a>:</h4>
<blockquote>
<p>The programmer is responsible, by using unsafe, to ensure no aliasing within the shader.</p>
</blockquote>
<p>This should be just " The programmer is responsible, by using unsafe, to ensure no aliasing". Not only can the data be aliased by threads in other shaders, but also threads in the CPU could be writing to the date while the GPU shader is reading it.</p>



<a name="239138039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239138039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Mobius <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239138039">(May 17 2021 at 18:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20.28it.20isn't.20yet.29/near/239115317">said</a>:</p>
<blockquote>
<p>I guess I don't understand why one has to wait for a <code>Buffer</code> type to be introduced.</p>
<p>You can use <code>usize</code> or <code>*mut [T]</code> or <code>NonNull&lt;[T]&gt;</code> today and that would be sound.</p>
<p>But there is no <code>T</code> for which using <code>&amp;mut T</code> isn't instant undefined behavior (unless the compute shader only has 1 thread).</p>
</blockquote>
<p>Work is being done on introducing synchronization primitives to rust-gpu, I have to imagine that's the rationale for the Buffer type.</p>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20.28it.20isn't.20yet.29/near/239115868">said</a>:</p>
<blockquote>
<blockquote>
<p>The programmer is responsible, by using unsafe, to ensure no aliasing within the shader.</p>
</blockquote>
<p>This should be just " The programmer is responsible, by using unsafe, to ensure no aliasing". Not only can the data be aliased by threads in other shaders, but also threads in the CPU could be writing to the date while the GPU shader is reading it.</p>
</blockquote>
<p>Arguably, keeping a memory range mapped while it's in use by the compute shader is unsound in and of itself - not sure if that's a valid use case.</p>



<a name="239165528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239165528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239165528">(May 17 2021 at 21:51)</a>:</h4>
<blockquote>
<p>I have to imagine that's the rationale for the Buffer type.</p>
</blockquote>
<p>Do they want to expose a different buffer for every synchronization pattern in existance ? <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span>  </p>
<blockquote>
<p>keeping a memory range mapped while it's in use by the compute shader is unsound in and of itself </p>
</blockquote>
<p>Why? A thread is a thread is a thread. Hardware support for GPU-CPU atomics was introduced a decade ago, and a lot of hardware today has coherent CPU-GPU fabrics.</p>
<blockquote>
<p>not sure if that's a valid use case.</p>
</blockquote>
<p>There are many apps doing this. For example, a compute kernel that processes a stream of data, using CPU-GPU atomics to pull data from a work queue (e.g. CPU copies data to GPU, signals it as ready, and copies output out, but the shader never stops working).</p>



<a name="239165565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239165565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239165565">(May 17 2021 at 21:51)</a>:</h4>
<p>Another simple example is... reading a file from the GPU. The CPU can mmap it, and the GPU can just read it.</p>



<a name="239165694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239165694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239165694">(May 17 2021 at 21:52)</a>:</h4>
<p>Another example would be doing network communication from a compute shader. The shader can pack/unpack data very quickly, and either directly write to the memory of another GPU (e.g. via a global address space), or signal the CPU to initiate a network transfer of that data (via a task queue).</p>



<a name="239165866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239165866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239165866">(May 17 2021 at 21:54)</a>:</h4>
<p>Lots of examples, but maybe even more generally, every app for which the latency of launching a shader is not acceptable just solves the problem by keeping the shader running, and that means that threads within the shader need to communicate with threads outside the shader using atomics.</p>



<a name="239166105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239166105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Mobius <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239166105">(May 17 2021 at 21:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20.28it.20isn't.20yet.29/near/239165528">said</a>:</p>
<blockquote>
<blockquote>
<p>I have to imagine that's the rationale for the Buffer type.</p>
</blockquote>
<p>Do they want to expose a different buffer for every synchronization pattern in existance ? <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span>  </p>
</blockquote>
<p>Hm? I mean, being able to safely express <em>some</em> synchronization patterns is better than <em>none</em> - Rust cannot safely express any memory management pattern safely, for example, but it covers common use-cases.</p>



<a name="239166108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239166108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239166108">(May 17 2021 at 21:56)</a>:</h4>
<blockquote>
<p>keeping a memory range mapped while it's in use by the compute shader is unsound in and of itself </p>
</blockquote>
<p>You can actually prove this is sound in a couple of memory models.</p>



<a name="239166146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239166146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239166146">(May 17 2021 at 21:57)</a>:</h4>
<blockquote>
<p>Hm? I mean, being able to safely express some synchronization patterns is better than none - Rust cannot safely express any memory management pattern safely, for example, but it covers common use-cases.</p>
</blockquote>
<p>Right, but there are thousands of different patterns on GPUs, pretty much every app uses a different pattern.</p>



<a name="239166283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239166283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239166283">(May 17 2021 at 21:58)</a>:</h4>
<p>Just look at classical parallel algorithms on GPUs (scan, histograms, sort, etc.). You find a dozen different patterns in each.</p>



<a name="239166547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239166547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239166547">(May 17 2021 at 22:01)</a>:</h4>
<p>If they want to wait for having types for "the basic" patterns before starting to consider rust-gpu sound... they are going to be waiting for a long time...</p>



<a name="239835232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/239835232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> riking <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#239835232">(May 22 2021 at 02:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20.28it.20isn't.20yet.29/near/239165565">said</a>:</p>
<blockquote>
<p>Another simple example is... reading a file from the GPU. The CPU can mmap it, and the GPU can just read it.</p>
</blockquote>
<p>Not quite that simple - the GPU needs to be mindful of the IOMMU, so the kernel needs to mark the memory as accessible to the GPU too.</p>



<a name="241672584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F%20%28it%20isn%27t%20yet%29/near/241672584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Demi Obenour <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.20(it.20isn&#x27;t.20yet).html#241672584">(Jun 06 2021 at 04:46)</a>:</h4>
<p>For something like <code>rust-gpu</code>, I imagine the best approach is to use an opaque buffer type that doesn’t allow obtaining references to the inside of the buffer.  Fuschia has something similar, but IMO it belongs in the standard library so that we can expose <code>mmap</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>